var redg = -1080;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [],
    originData: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      sn: options.eq_sn
      // sn: 'f56a0130bb8ee5'
    });
    that.getControlParam();
  },
  /**
   * 获取控制参数
   */
  getControlParam() {
    var that = this;
    app.ajax({
      url: 'Manage/Equipment/getControlParam',
      method: "POST",
      data: {
        sn: that.data.sn
      },
      success: res => {
        if (res.data.code == 1000) {
          var data = res.data.data;
          data.forEach(item => {
            item.animationData = {};
            item.num = item.value;
          })
          that.setData({
            listArr: data,
            originData: JSON.parse(JSON.stringify(data))
            // data: data
          });
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },


  //选择badge
  chooseBadge(e) {
    const {
      index,
      value,
      idx
    } = e.currentTarget.dataset;
    const arr = this.data.listArr;
    arr[index].num = value;
    const options = arr[index].option;
    options.forEach((current, i) => { //设置options中的checked值
      current.checked = i == idx ? true : false
    })
    this.setData({
      listArr: arr
    })
  },

  //增加
  add: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum < 9999999) {
      newArr[indnum].num++
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //减少
  cut: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum > 0) {
      newArr[indnum].num--
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //input输入
  inputChnge: function (e) {
    var that = this;
    var inpval = e.detail.value;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    if (inpval >= 0) {
      newArr[indnum].num = inpval;
      that.setData({
        listArr: newArr
      })
    }
  },
  //刷新
  updata: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    if (redg) {
      redg -= 1080;
    }
    that.animation = animation
    animation.rotate(redg).step()
    newArr[indnum].animationData = animation //控制对应的动画
    var that = this;
    that.setData({
      listArr: newArr
    })
    //请求接口
    app.ajax({
      url: 'Manage/Equipment/controlParamEdit',
      method: "POST",
      data: {
        sn: that.data.sn,
        param: newArr[indnum].id,
        value: newArr[indnum].num
      },
      success: function (res) {
        if (res.data.code == 1000) {
          const originData = that.data.originData;
          originData[indnum].num = newArr[indnum].num;
          setTimeout(() => {
            that.setData({
              originData
            })
          }, 2000)
          app.showToast(res.data.message, "none", 2000, function () { });
        } else if (res.data.code == -1000) {
          app.showToast(res.data.message, "none", 2000, function () { });
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  }
})